package com.homedo.async.mysql.support.dao;

import com.google.common.collect.Lists;

import java.util.List;
import java.util.function.Function;

/**
 * @author jgshun
 * @date 2020/1/20 10:54 AM
 */
public abstract class TypeChecker {

    private static List<Class> primitiveTypes = Lists.newArrayList();
    private static List<Class> numericTypes = Lists.newArrayList();
    private static List<Class> dateTypes = Lists.newArrayList();
    private static List<Class> stringTypes = Lists.newArrayList();
    private static List<Class> jsonTypes = Lists.newArrayList();
    private static List<Class> bufferTypes = Lists.newArrayList();

    static {
        primitiveTypes.add(byte.class);
        primitiveTypes.add(short.class);
        primitiveTypes.add(int.class);
        primitiveTypes.add(long.class);
        primitiveTypes.add(float.class);
        primitiveTypes.add(double.class);
        primitiveTypes.add(boolean.class);
        primitiveTypes.add(Byte.class);
        primitiveTypes.add(Short.class);
        primitiveTypes.add(Integer.class);
        primitiveTypes.add(Long.class);
        primitiveTypes.add(Float.class);
        primitiveTypes.add(Double.class);
        primitiveTypes.add(Boolean.class);

        numericTypes.add(io.vertx.sqlclient.data.Numeric.class);
        numericTypes.add(java.math.BigDecimal.class);

        dateTypes.add(java.time.LocalDate.class);
        dateTypes.add(java.time.LocalDateTime.class);
        dateTypes.add(java.time.Duration.class);
        dateTypes.add(java.time.LocalTime.class);
        dateTypes.add(java.util.Date.class);

        stringTypes.add(char.class);
        stringTypes.add(Character.class);
        stringTypes.add(String.class);

        jsonTypes.add(io.vertx.core.json.JsonObject.class);
        jsonTypes.add(io.vertx.core.json.JsonArray.class);

        bufferTypes.add(io.vertx.core.buffer.Buffer.class);
    }

    public static TypeEnum checkType(Class claz) {
        if (primitiveTypes.contains(claz)) {
            return TypeEnum.PRIMITIVE;
        }

        if (numericTypes.contains(claz)) {
            return TypeEnum.NUMERIC;
        }

        if (dateTypes.contains(claz)) {
            return TypeEnum.DATE;
        }

        if (stringTypes.contains(claz)) {
            return TypeEnum.STRING;
        }

        if (jsonTypes.contains(claz)) {
            return TypeEnum.JSON;
        }

        if (bufferTypes.contains(claz)) {
            return TypeEnum.BUFFER;
        }

        return TypeEnum.UNKNOWN;
    }

    public static String computeColumn(Class fieldClaz, Function<Class, String> function) {
        return function.apply(fieldClaz);
    }

    public enum TypeEnum {
        PRIMITIVE,
        NUMERIC,
        DATE,
        STRING,
        JSON,
        BUFFER,
        UNKNOWN,
    }

}
